function CardSlider($container) {
	this._$container = $container;
	this._cardList = new Array();
	this._defaultCardIndex = 0;
	this.selectedBankCardId = 0;
	this.init();
}
CardSlider.prototype.init = function() {
	var me = this;
	me.render();
	this.getCardData();
	//for IE8 bug
	setTimeout(function(){
		$('.cardlist').css('zoom',1);
	},500);
};

CardSlider.prototype.getCardData = function() {
	var me = this;
	$.post('/bank/getbankcard',function(data){
		if(data && data.error_no ==0) {
			me.cardDataList = data.data;
			me.initAddCard();
			if(data.data.length == 0) {
				$('.cardlist').hide();
				$('.add_card').css('margin-right','360px');
				$('.add_card').css('_margin-right','180px');
			} else {
				if(5 <= data.data.length) {
					$('.add_card').hide();
					$('.cardlist').css('margin-left','135px');
				}
				me.buildCardList();
			}	
		} else {
			alert(data.error_message);
		}
	},'json');
};
CardSlider.prototype.render = function() {
	var template = __inline("./cardslider.tmpl");
    var content = template({});
    this._$html = $(content);
    this._$container.append(this._$html);
};
CardSlider.prototype.buildCardList = function() {
	var me = this;
	var Card = require('home:widget/ui/card/card.js');
	for (var i = 0; i < me.cardDataList.length; i++) {
		var card = new Card(me.cardDataList[i]);
		me._cardList.push(card);
		card.render($('.cardlist ul'), i);
	};
	//设定默认的mask顺序，避免重叠
	for (var i = 0; i < me._cardList.length; i++) {
		var tmp  = me._cardList.length + 5 - i;
		me._cardList[i].setZIndex(tmp);
	};
	me.eventCenter();
}
CardSlider.prototype.eventCenter = function() {
	var me = this,
	mouseoverIndexEvent = function(event,data) {
		for (var i = 0; i < me._cardList.length; i++) {
			if(i != parseInt(data.index)) {
				var tmp  = 5 - Math.abs(i - parseInt(data.index));
				me._cardList[i].setZIndex(tmp);
			}
		};
	};
	mouseoutIndexEvent = function(event,data) {
		for (var i = 0; i < me._cardList.length; i++) {
			me._cardList[i].resetZIndex();
		};
	};
	clickCardEvent = function(event,data) {
		me.selectedBankCardId = me._cardList[data.index]['_bank_card_id'];
		for (var i = 0; i < me._cardList.length; i++) {
			me._cardList[i].setFlag(data.index);
			me._cardList[i].resetMask(data.index);
		};	
	}
	this._$html.bind('mouseoverIndex',mouseoverIndexEvent);
	this._$html.bind('mouseoutIndex',mouseoutIndexEvent);
	this._$html.bind('clickCard',clickCardEvent);
};

CardSlider.prototype.initAddCard = function () {
	var me = this;
	$('.add_card').show();
	$('.add_card').bind('click',function () {
		if(!me.bankcardlayer) {
			var BankCardLayer = require('home:widget/ui/BankCardLayer/BankCardLayer.js');
			me.bankcardlayer = new BankCardLayer();
		}
		me.bankcardlayer.show();
		$('#BankCardLayer_submit').bind('click',function() {
	        if(!me.bankcardlayer.checkForm()) {
	        	return;
	        }
	        var opt = {'bank_type':$('#bank_type').val(),'bankcard_number':$('#bankcard_number').val()};
            $.post('/bank/bindbankcard', opt ,function(data) {
	            if(data && data.error_no == 0) {
	                alert('银行卡添加完成。');
	                location.reload();
	            } else {
	                me.bankcardlayer.setError(data.error_message);
	            }
	        },'json');
	    })
	});
}

module.exports = CardSlider;
